package com.xyl.audio;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;

import com.anthonycr.grant.PermissionsManager;
import com.anthonycr.grant.PermissionsResultAction;
import com.lansosdk.videoeditor.CopyFileFromAssets;
import com.lansosdk.videoeditor.LanSongFileUtil;
import com.xyl.audio.utils.AudioUtils;
import com.xyl.audio.ximalaya.XmlyRecordActivity;

public class MainActivity extends BaseActivity {

    private AudioUtils audioEditor;
    private String filePath;
    private ProgressDialog mProgressDialog;
    private boolean isRunning = false;
    private boolean isPermissionOk = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkPermissions();

        //1、首先创建audioEditor对象
        audioEditor = new AudioUtils();
        //2、设置进度监听
        audioEditor.setOnAudioEditorProgressListener(new AudioUtils.onAudioEditorProgressListener() {

            @Override
            public void onProgress(AudioUtils v, int percent) {
                if (mProgressDialog != null) {
                    mProgressDialog.setMessage("正在处理中..." + percent + "%");
                }
            }
        });
    }

    private void checkPermissions() {
        PermissionsManager.getInstance().requestAllManifestPermissionsIfNecessary(this, new PermissionsResultAction() {
            @Override
            public void onGranted() {
                isPermissionOk = true;
                CopyFileFromAssets.copyAssets(MainActivity.this, "hongdou20s.mp3");
                filePath = LanSongFileUtil.getPath() + "/" + "hongdou20s.mp3";
            }

            @Override
            public void onDenied(String permission) {
                isPermissionOk = false;
            }
        });
    }

    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.btn1://
                startActivity(new Intent(this, XmlyRecordActivity.class));
                break;
            case R.id.btn2://
//                if (!isRunning) {
//                    //4、执行
//                    new SubAsyncTask().execute();
//                }
                break;
            default:
                break;
        }
    }

    /**
     * 3、开启异步执行 audioEditor方法
     */
    public class SubAsyncTask extends AsyncTask<Object, Object, Boolean> {
        @Override
        protected void onPreExecute() {
            showProgressDialog();
            isRunning = true;
            super.onPreExecute();
        }

        @Override
        protected synchronized Boolean doInBackground(Object... params) {
            //执行此方法需开启异步执行
            //音频解码成pcm
            //audioEditor.executeConvertToPCM(filePath, -1);
            //两个pcm合成的同时能够调节音量
//            audioEditor.executePcmMix(
//                    LanSongFileUtil.getPath() + "/" + "19716133820425.pcm",44100,1,
//                    LanSongFileUtil.getPath() + "/" + "19716134248158.pcm",44100,1,1.0f, 0.0f);
            //合成的pcm文件转成mp3

//            audioEditor.executePcmEncodeMp3(
//                    LanSongFileUtil.getPath() + "/" + "1971614194529.pcm",
//                    44100,1,0);
//            audioEditor.executePcmEncodeM4a(
//                    LanSongFileUtil.getPath() + "/" + "1971614194529.pcm",
//                    44100,1,0);
//            audioEditor.executePcmEncodeAac(
//                    LanSongFileUtil.getPath() + "/" + "1971614194529.pcm",
//                    44100,1);
//            String[] audioArr = new String[]{
//                    LanSongFileUtil.getPath() + "/" + "hongdou20s.mp3",
//                    LanSongFileUtil.getPath() + "/" + "hongdou20s.mp3"};
//            audioEditor.concatAudio(audioArr
//                    , LanSongFileUtil.createMP3FileInBox());
            audioEditor.executeCutAudio(LanSongFileUtil.getPath() + "/" + "hongdou20s.mp3", 0, 30);
            return null;

        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            cancelProgressDialog();
            isRunning = false;

        }
    }

    /**
     * 显示进度
     */
    private void showProgressDialog() {
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setMessage("正在处理中...");
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
    }

    /**
     * 取消进度
     */
    private void cancelProgressDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.cancel();
            mProgressDialog = null;
        }
    }
}
